can.autorender
can/view/autorender
{function()}
A module that automatically renders script and other elements with the can-autorender attribute. This function is useful to know when the templates have finished rendering.
can.autorender(succcess, error)
Registers functions to callback when all templates successfully render or an error in rendering happens.
Parameters
-
success
{function()}
A function to callback when all autorendered templates have been rendered successfully.
-
error
{function()}
OptionalA function to callback if a template was not rendered successfully.
Use
As long is this module is part of your CanJS build or imported with RequireJS, StealJS, or SystemJS, can-autorender will automatically look for
can-autorender
tags and render them. Once all templates have finished rendering, it will call any callbacks passed tocan.autorender()
.For example, you might have a page like:
Rendered placement
If the template source is a
<script>
tag within the<body>
, the rendered template is placed immediately following the template.For example:
Becomes:
If the
<script>
tag is outside the body, for example in the<head>
tag, the rendered result will be placed just before the closing</body>
tag.For example:
Becomes:
If the template source is any other element, the element's contents will be replaced with the rendered result. For example:
Becomes:
Scope
The template is rendered with a can.Map made from the attributes of the template source element. That
map
is available on the template source element via can.viewModel. You can change the map at any time:You can change attributes on the element and it will update the viewModel too:
StealJS Use
For demo pages that require very little setup:
For demo pages that require a little custom setup:
Errors
Error callbacks will be called if a template has a parsing error or a fails.